    //
//  ChangePasswordViewController.m
//  iShop
//
//  Created by svp on 1/10/12.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "ChangePasswordViewController.h"
#import "ResourceLoader.h"
#import "TitleTextfieldCell.h"
#import "ConstansDefine.h"
#import "ChangePasswordRequest.h"
#import "iShopAppDelegate.h"
#import "Utils.h"

@implementation ChangePasswordViewController

@synthesize tableView = _tableView;
@synthesize btnDoFunction = _btnDoFunction;
@synthesize btnCancel = _btnCancel;

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
		infomation = [[NSMutableDictionary alloc] init];
		[infomation setValue:@"" forKey:kCurrentPassword];
		[infomation setValue:@"" forKey:kNewPassword];
		[infomation setValue:@"" forKey:kConfirmPassword];
    }
    return self;
}

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return 1;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
	return @"Account";
	
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*) indexPath
{
	return 44;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	TitleTextfieldCell* cell = (TitleTextfieldCell*)[tableView dequeueReusableCellWithIdentifier:@"TitleTextfieldCell"];
	if (!cell) {
		ResourceLoader *rc = [[[ResourceLoader alloc] initWithNibNamed:@"TitleTextfieldCell"] autorelease];
		cell = (TitleTextfieldCell*)rc.resource;
		cell.delegate = self;
		cell.textField.secureTextEntry = YES;
	}
	cell.tag = indexPath.row;
	cell.textField.text = @"";
	NSString *key = @"";
	switch (indexPath.row) {
		case 0:
			cell.titleLabel.text = @"Current Password";
			key = kCurrentPassword;
			break;
		case 1:
			cell.titleLabel.text = @"New password";
			key = kNewPassword;
			break;
		case 2:
			cell.titleLabel.text = @"Confirm";
			key = kConfirmPassword;
			break;
		default:
			break;
	}
	cell.textField.text = [infomation valueForKey:key];
	return cell;
}

-(void) textFieldDidEndEditing:(UITableViewCell*)cell
{
	
	switch (cell.tag) {
		case 0:
			[infomation setObject:[((TitleTextfieldCell*)cell) cellValue] forKey:kCurrentPassword];
			break;
		case 1:
			[infomation setObject:[((TitleTextfieldCell*)cell) cellValue] forKey:kNewPassword];
			break;
		case 2:
			[infomation setObject:[((TitleTextfieldCell*)cell) cellValue] forKey:kConfirmPassword];
			break;
		default:
			break;
	}
}

-(void) viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
}

-(void) viewDidLoad
{
	[super viewDidLoad];
	UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(endEditTable)];
	
	// For selecting cell.
	gestureRecognizer.cancelsTouchesInView = NO;
	
	[_tableView addGestureRecognizer:gestureRecognizer];
	
	[gestureRecognizer release];
}

-(void) endEditTable
{
	[_tableView endEditing:YES];
}

-(void) showAlertText:(NSString*)text
{
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Infomation" message:text delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
	[alert show];
	[alert release];
}

-(IBAction) doFunctionPress:(id) sender
{
	[_tableView endEditing:YES];
	//check info
	NSLog(@"test:%@,%@,%@",[Utils getValueForRefKey:KPassword],[infomation valueForKey:kNewPassword],[infomation valueForKey:kConfirmPassword]);
	if (![((NSString*)[Utils getValueForRefKey:KPassword]) isEqualToString:[infomation valueForKey:kCurrentPassword]]) {
		[self showAlertText:@"Your current password is incorrect"];
		return;
	}
	
	if (![((NSString*)[infomation valueForKey:kNewPassword]) isEqualToString:[infomation valueForKey:kConfirmPassword]]) {
		[self showAlertText:@"New password does not match"];
	}
	
	[((iShopAppDelegate*)[[UIApplication sharedApplication] delegate]) startSpinner:@"Waiting"];
	
	ChangePasswordRequest *request = [[ChangePasswordRequest alloc] init];
	request.delegate = self;
	[request makeRequest:infomation];
	
	//[self.parentViewController dismissModalViewControllerAnimated:YES];
}

-(void) processRequestResult:(BOOL)result
{
	[((iShopAppDelegate*)[[UIApplication sharedApplication] delegate]) stopSpinner];
	if (result) {
		[Utils setValue:[infomation valueForKey:kNewPassword] forRefKey:kPassword];
		[self.parentViewController dismissModalViewControllerAnimated:NO];
	}
}

-(IBAction) cancelPress:(id) sender
{
	[self.parentViewController dismissModalViewControllerAnimated:YES];
}



@end
